﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.ProviderBase;
using System.Data.Sql;
//using LLBLGenWebConsultationDAL;
//using LLBLGenWebConsultationDAL.CollectionClasses;
//using LLBLGenWebConsultationDAL.EntityClasses;
//using LLBLGenWebConsultationDAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using SD.LLBLGen.Pro.ORMSupportClasses.SelfServicingSpecific;
using SD.LLBLGen.Pro.DQE.SqlServer;
using System.Text.RegularExpressions;
using MyHealthOnline.Dal.Generated.CollectionClasses;
using MyHealthOnline.Dal.Generated.EntityClasses;
using MyHealthOnline.Dal.Generated.HelperClasses;
using System.Net.Mail;

namespace MyHealthOnline
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebServiceHO : System.Web.Services.WebService
    {

        public PatientCollection pacientColl;
        //adauga si verificare pt campul ACTIV
        #region LOGARE-Verifica daca exista utilizatorul cu rolul,username-ul si parola specificate
        [WebMethod]
        public int LoginByUsernameAndPassword(string role, string username, string password)
        {
            int login = 0; //presupunem ca nu exista un utilizator cu usernameul si parola specificate
            switch (role)
            {
                case "Administrator":
                    {
                        AdministratorCollection adminColl = new AdministratorCollection();
                        IPredicateExpression filter1 = new PredicateExpression();
                        filter1.Add(new FieldCompareValuePredicate(AdministratorFields.Email, ComparisonOperator.Equal, username.ToString()))
                                .AddWithAnd(new FieldCompareValuePredicate(AdministratorFields.Password, ComparisonOperator.Equal, password.ToString()));
                        //filter1.Add(AdministratorFields.Email == username);
                        //filter1.AddWithAnd(AdministratorFields.Password == password);
                        adminColl.GetMulti(filter1);// fetch all Customers
                        if (adminColl.Count != 0)
                            login = 1;
                        break;
                    }
                case "Patient":
                    {
                        pacientColl = new PatientCollection();
                        IPredicateExpression filter2 = new PredicateExpression();
                        filter2.Add(PatientFields.Email == username);
                        filter2.AddWithAnd(PatientFields.Password == password);
                        pacientColl.GetMulti(filter2);
                        if (pacientColl.Count != 0)
                            login = 1;
                        break;
                    }
                case "Specialist":
                    {
                        SpecialistCollection spColl = new SpecialistCollection();
                        IPredicateExpression filter3 = new PredicateExpression();
                        filter3.Add(SpecialistFields.Email == username);
                        filter3.AddWithAnd(SpecialistFields.Password == password);
                        spColl.GetMulti(filter3);
                        if (spColl.Count != 0)
                            login = 1;
                        break;
                    }
                default:
                    break;
            }
            return login;
        }
        #endregion

        #region Obtine id-ul administratorului cu emailul specificat
        [WebMethod]
        public int getIdAdmin(string email)
        {

            int Id = -1;
            try
            {

                AdministratorCollection adminCollection = new AdministratorCollection();
                AdministratorEntity adminEntity = new AdministratorEntity();

                IPredicateExpression filter = new PredicateExpression();
                filter.Add(AdministratorFields.Email == email);
                adminCollection.GetMulti(filter);
                if (adminCollection.Count > 0)
                {
                    AdministratorEntity adminEnt = adminCollection.ElementAt(0);

                    foreach (AdministratorEntity admin in adminCollection)
                    {
                        Id = admin.Id;
                    }
                }
            }
            catch (ORMFieldIsNullException e)
            {
                string error = e.StackTrace;
            }
            return Id;
        }
        #endregion

        #region Obtine id-ul Specialistului dupa email
        [WebMethod]
        public string getIdSpecialist(string email)
        {
            string Id = "";
            try
            {

                SpecialistCollection specialistColl = new SpecialistCollection();
                SpecialistEntity specialistEnt = new SpecialistEntity();

                IPredicateExpression filter = new PredicateExpression();
                filter.Add(SpecialistFields.Email == email);
                specialistColl.GetMulti(filter);
                if (specialistColl.Count > 0)
                {
                    SpecialistEntity spE = specialistColl.ElementAt(0);

                    foreach (SpecialistEntity specialist in specialistColl)
                    {
                        Id = specialist.Id;
                    }
                }
            }
            catch (ORMFieldIsNullException e)
            {
                string error = e.StackTrace;
            }
            return Id;
        }
        #endregion

        #region Obtine id-ul pacientului dupa email
        [WebMethod]
        public int getIdPatient(string email)
        {
            int Id = -1;
            try
            {

                pacientColl = new PatientCollection();
                PatientEntity pacientEnt = new PatientEntity();

                IPredicateExpression filter = new PredicateExpression();
                filter.Add(PatientFields.Email == email);
                pacientColl.GetMulti(filter);
                if (pacientColl.Count > 0)
                {
                    PatientEntity P = pacientColl.ElementAt(0);

                    foreach (PatientEntity pacient in pacientColl)
                    {
                        Id = pacient.Id;
                    }
                }
            }
            catch (ORMFieldIsNullException e)
            {
                string error = e.StackTrace;
            }
            return Id;
        }
        #endregion

        #region
        [WebMethod]
        public bool verificaSintaxaMail(string mail)
        {
            //regEx.Pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
            string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
          + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
          + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
          + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
          + @"[a-zA-Z]{2,}))$";
            Regex reStrict = new Regex(patternStrict);
            bool isStrictMatch = reStrict.IsMatch(mail);
            if (isStrictMatch) 
                { return true; }
            return false;
        }
        #endregion

        #region returneaza numele tuturor specialistilor
        [WebMethod]
        public string[] getSpecialistsByName(string prefixText, int count)
        {

            //ArrayList spNameArray = new ArrayList() ;
            SpecialistCollection spColl = new SpecialistCollection();
            spColl.GetMulti(null);
            int i = 0;
            int nrSpecialisti = spColl.Count;
            string[] spName = new string[nrSpecialisti];
            foreach (SpecialistEntity spEntity in spColl)
            {
                spName[i] = spEntity.Name.ToString();
                i++;
                // spNameArray.Add(spEntity.Name);   
            }
            return spName;
        }
        #endregion


        [WebMethod]
        public bool Notify(string mailFrom, string mailTo, string messageBody, string subject)
        {
            try
            {
                if (!verificaSintaxaMail(mailFrom) || !verificaSintaxaMail(mailTo))
                {
                    return false;
                }

                MailMessage mail = new MailMessage(
                                    mailFrom,
                                    mailTo);
                mail.Body = messageBody;
                mail.Subject = subject;

                SmtpClient smtp = new SmtpClient();
                smtp.EnableSsl = true;
                smtp.Send(mail);

                mail.Dispose();
                smtp.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
